<?php
/**
* MenusView.php
* Handles views for menu handling.
*/

class MenusView
{
    /**
    * Takes the input from user
    *
    */
    public function DrawNewMenuForm()
    {
        $html = '';

        $html .= '<form action="" method="POST">';
        $html .= '    <fieldset>';
        $html .= '        <label for="add_name">Menu Name:</label>';
        $html .= '        <input type="text" name="add_name" id="add_name"/>';
        $html .= '        <input type="submit" name="add_save" value="Add" />';
        $html .= '    </fieldset>';
        $html .= '</form>';

        return $html;
    }

    /**
    * Show all menu from the database
    *
    * @param Array $menuArray containing menu references from database
    *
    */
    public function DrawMenusList( $menus )
    {
        $html = '';
        
        $html .= '<table>' . "\r\n";
        $html .= '    <thead>' . "\r\n";
        $html .= '        <tr>' . "\r\n";
        $html .= '            <th>MenuID</th>' . "\r\n";
        $html .= '            <th>Name</th>' . "\r\n";
        $html .= '            <th>Action</th>' . "\r\n";
        $html .= '        </tr>' . "\r\n";
        $html .= '    </thead>' . "\r\n";
        
        $html .= '    <tbody>' . "\r\n";
        
        $menuCount = count( $menus );
        for( $i = 0; $i < $menuCount; $i++ )
        {
            $html .= '        <tr>' . "\r\n";
            $html .= '            <td>' . $menus[$i]->GetMenuID() . '</td>' . "\r\n";
            $html .= '            <td>' . $menus[$i]->GetName() . '</td>' . "\r\n";
            $html .= '            <td>' . "\r\n";
            $html .= '                <form action="" method="POST">' . "\r\n";
            $html .= '                    <fieldset>' . "\r\n";
            $html .= '                        <input type="hidden" name="list_menuID" value="' . $menus[$i]->GetMenuID() . '" />' . "\r\n";
            $html .= '                        <input type="hidden" name="list_name" value="' . $menus[$i]->GetName() . '" />' . "\r\n";
            $html .= '                        <input type="submit" name="list_editOption" value="Edit" />' . "\r\n";
            $html .= '                        <input type="submit" name="list_editOption" value="Delete" />' . "\r\n";
            $html .= '                    </fieldset>' . "\r\n";
            $html .= '                </form>' . "\r\n";
            $html .= '            </td>' . "\r\n";
            $html .= '        </tr>' . "\r\n";
        }
        $html .= '    </tbody>' . "\r\n";
        
        $html .= '</table>' . "\r\n";

        return $html;
    }

	public function DrawEditMenuForm( $menu )
	{
		$html = '';
        
		$html .= '<form action="" method="POST">' . "\n\r";
        $html .= '    <fieldset>' . "\n\r";
		$html .= '        <input type="hidden" name="edit_menuID" value="' . $menu->GetMenuID() . '" />' . "\r\n";
        
		$html .= '        <label for="edit_name">Menu Name:</label>' . "\n\r";
		$html .= '        <input type="text" name="edit_name" id="edit_name" value="' . $menu->GetName() . '" />' . "\r\n";

		$html .= '        <input type="submit" name="edit_save" value="Save" /> <input type="submit" name="edit_save" value="Cancel" />' . "\r\n";
        $html .= '    </fieldset>' . "\n\r";
		$html .= '</form>' . "\r\n";
		
		return $html;
	}
    
    public function DrawAddPathForm( $menuID, $pages )
    {
        $html = '';
        
        $html .= '<form action="" method="POST">';
        $html .= '    <fieldset>';
        $html .= '        <input type="hidden" name="addPath_menuID" value="' . $menuID . '" />';

        $html .= '        <label for="addPath_text">Path Name:</label>';
        $html .= '        <input type="text" name="addPath_text" id="addPath_text" />';
        
        // Keep data for the menu to edit.
        $html .= '        <input type="hidden" name="list_editOption" value="' . $this->GetListMenuValue() . '" />' . "\r\n";
        $html .= '        <input type="hidden" name="list_menuID" value="' . $this->GetListMenuID() . '" />' . "\r\n";
        $html .= '        <input type="hidden" name="list_name" value="' . $this->GetListMenuName() . '" />' . "\r\n";
            
        $html .= '        <label for="addPath_pageID">Page:</label>';
        $html .= '        <select name="addPath_pageID" id="addPath_pageID">';
        foreach ( $pages as $page )
        {
            $html .= '            <option value="' . $page['id'] . '">' . $page['title'] . '</option>';
        }
        $html .= '        </select>';

        $html .= '        <input type="submit" name="addPath_add" value="Add Menu Item" />';
        $html .= '    </fieldset>';
        $html .= '</form>';
        
        return $html;
    }
    
    public function DrawMenuItemsList( $paths )
    {
        $html = '';
        
        $html .= '<table>' . "\r\n";
        $html .= '    <thead>' . "\r\n";
        $html .= '        <tr>' . "\r\n";
        $html .= '            <th>Order</th>' . "\r\n";
        $html .= '            <th>Name</th>' . "\r\n";
        $html .= '            <th>Text</th>' . "\r\n";
        $html .= '            <th>Page</th>' . "\r\n";
        $html .= '            <th>Actions</th>' . "\r\n";
        $html .= '        </tr>' . "\r\n";
        $html .= '    </thead>' . "\r\n";
        
        $html .= '    <tbody>' . "\r\n";

        $html .= $this->AddMenuItemListRows( $paths );

        $html .= '    </tbody>' . "\r\n";
        
        $html .= '</table>' . "\r\n";

        return $html;
    }

    private function AddMenuItemListRows( $paths, $indent = '' )
    {
        $html = '';
        
        foreach ( $paths as $path )
        {
            
            $html .= '        <tr>' . "\r\n";
            $html .= '            <td>' . $indent . $path['order'] . '</td>' . "\r\n";
            $html .= '            <td>' . $path['name'] . '</td>' . "\r\n";
            $html .= '            <td>' . $path['text'] . '</td>' . "\r\n";
            $html .= '            <td>' . $path['pageID'] . '</td>' . "\r\n";
            $html .= '            <td>' . "\r\n";
            $html .= '                <form action="" method="POST">' . "\r\n";
            $html .= '                    <fieldset>' . "\r\n";
            $html .= '                        <input type="hidden" name="listpaths_pathID" value="' . $path['id'] . '" />' . "\r\n";
            $html .= '                        <input type="hidden" name="listpaths_pathName" value="' . $path['name'] . '" />' . "\r\n";
            
            // Keep data for the menu to edit.
            $html .= '                        <input type="hidden" name="list_editOption" value="' . $this->GetListMenuValue() . '" />' . "\r\n";
            $html .= '                        <input type="hidden" name="list_menuID" value="' . $this->GetListMenuID() . '" />' . "\r\n";
            $html .= '                        <input type="hidden" name="list_name" value="' . $this->GetListMenuName() . '" />' . "\r\n";
            
            $html .= '                        <input type="submit" name="listpaths_editOption" value="Up" />' . "\r\n";
            $html .= '                        <input type="submit" name="listpaths_editOption" value="Down" />' . "\r\n";

            $html .= '                        <input type="submit" name="listpaths_editOption" value="In" />' . "\r\n";
            $html .= '                        <input type="submit" name="listpaths_editOption" value="Out" />' . "\r\n";
            
            $html .= '                        <input type="submit" name="listpaths_editOption" value="Edit" />' . "\r\n";
            $html .= '                        <input type="submit" name="listpaths_editOption" value="Delete" />' . "\r\n";
            $html .= '                    </fieldset>' . "\r\n";
            $html .= '                </form>' . "\r\n";
            $html .= '            </td>' . "\r\n";
            $html .= '        </tr>' . "\r\n";
            
            if ( $path['children'] != NULL )
            {
                $html .= $this->AddMenuItemListRows( $path['children'], $indent . '&nbsp;&nbsp;&nbsp;' );
            }
        }
        
        return $html;
    }
    
    public function DrawEditMenuItemForm( $menuItem, $pages )
	{
		$html = '';
        
		$html .= '<form action="" method="POST">' . "\n\r";
        $html .= '    <fieldset>' . "\n\r";
		$html .= '        <input type="hidden" name="editMenuItem_pathID" value="' . $menuItem['id'] . '" />' . "\r\n";
        
        // Keep data for the menu to edit.
        $html .= '        <input type="hidden" name="list_editOption" value="' . $this->GetListMenuValue() . '" />' . "\r\n";
        $html .= '        <input type="hidden" name="list_menuID" value="' . $this->GetListMenuID() . '" />' . "\r\n";
        $html .= '        <input type="hidden" name="list_name" value="' . $this->GetListMenuName() . '" />' . "\r\n";

		$html .= '        <label for="editMenuItem_name">Path Name:</label>' . "\n\r";
		$html .= '        <input type="text" name="editMenuItem_name" id="editMenuItem_name" value="' . $menuItem['name'] . '" />' . "\r\n";

		$html .= '        <label for="editMenuItem_text">Menu Item Text:</label>' . "\n\r";
		$html .= '        <input type="text" name="editMenuItem_text" id="editMenuItem_text" value="' . $menuItem['text'] . '" />' . "\r\n";
        
        $html .= '        <label for="editMenuItem_pageID">Page:</label>' . "\r\n";
        $html .= '        <select name="editMenuItem_pageID" id="addPath_pageID">';
        foreach ( $pages as $page )
        {
            $html .= '            <option value="' . $page['id'] . '"';
            if ( $page['id'] == $menuItem['pageID'] )
            {
                $html .= ' selected="selected"';
            }
            $html .= '>' . $page['title'] . '</option>';
        }
        $html .= '        </select>';
        
		$html .= '        <input type="submit" name="editMenuItem_save" value="Save" /> <input type="submit" name="editMenuItem_save" value="Cancel" />' . "\r\n";
        $html .= '    </fieldset>' . "\n\r";
		$html .= '</form>' . "\r\n";
		
		return $html;
	}
    
    ## START: ADD NEW MENU FORM ##
    
    public function GetAddMenuOption()
    {
        return isset( $_POST['add_save'] ) ? $_POST['add_save'] : NULL;
    }

    public function GetAddMenuName()
    {
        return isset( $_POST['add_name'] ) ? $_POST['add_name'] : NULL;
    }

    ## END: ADD NEW MENU FORM ##
    
    ## START: MENUS LIST ##
        
    public function GetListMenuOption()
    {
        return isset( $_POST['list_editOption'] ) ? $_POST['list_editOption'] : NULL;
    }

    public function GetListMenuValue()
    {
        return isset( $_POST['list_editOption'] ) ? $_POST['list_editOption'] : NULL;
    }
    
    public function GetListMenuID()
    {
        return isset( $_POST['list_menuID'] ) ? $_POST['list_menuID'] : NULL;
    }
    
    public function GetListMenuName()
    {
        return isset( $_POST['list_name'] ) ? $_POST['list_name'] : NULL;
    }
    
    ## END: MENUS LIST ##
    
    ## START: EDIT MENU FORM ##
    
    public function GetEditMenuOption()
    {
        return isset( $_POST['edit_save'] ) ? $_POST['edit_save'] : NULL;
    }
    
    public function GetEditMenuID()
    {
        return isset( $_POST['edit_menuID'] ) ? $_POST['edit_menuID'] : NULL;
    }
    
    public function GetEditMenuName()
    {
        return isset( $_POST['edit_name'] ) ? $_POST['edit_name'] : NULL;
    }
    
    ## END: EDIT MENU FORM ##
    
    ## START: ADD MENU ITEM/PATH FORM ##
    
    public function GetAddMenuItemOption()
    {
        return isset( $_POST['addPath_add'] ) ? $_POST['addPath_add'] : NULL;
    }
    
    public function GetAddMenuItemID()
    {
        return isset( $_POST['addPath_menuID'] ) ? $_POST['addPath_menuID'] : NULL;
    }
    
    public function GetAddMenuItemText()
    {
        return isset( $_POST['addPath_text'] ) ? $_POST['addPath_text'] : NULL;
    }
    
    public function GetAddMenuItemPageID()
    {
        return isset( $_POST['addPath_pageID'] ) ? $_POST['addPath_pageID'] : NULL;
    }
    
    ## END: ADD MENU ITEM/PATH FORM ##
    
    ## START: MENU ITEMS LIST ##
    
    public function GetListMenuItemOption()
    {
        return isset( $_POST['listpaths_editOption'] ) ? $_POST['listpaths_editOption'] : NULL;
    }
    
    public function GetListMenuItemID()
    {
        return isset( $_POST['listpaths_pathID'] ) ? $_POST['listpaths_pathID'] : NULL;
    }
    
    ## END: MENU ITEMS LIST ##
    
    ## START: EDIT MENU ITEM ##
    
    public function GetEditMenuItemOption()
    {
        return isset( $_POST['editMenuItem_save'] ) ? $_POST['editMenuItem_save'] : NULL;
    }
    
    public function GetEditMenuItemID()
    {
        return isset( $_POST['editMenuItem_pathID'] ) ? $_POST['editMenuItem_pathID'] : NULL;
    }
    
    public function GetEditMenuItemName()
    {
        return isset( $_POST['editMenuItem_name'] ) ? $_POST['editMenuItem_name'] : NULL;
    }
    
    public function GetEditMenuItemText()
    {
        return isset( $_POST['editMenuItem_text'] ) ? $_POST['editMenuItem_text'] : NULL;
    }
    
    public function GetEditMenuItemPageID()
    {
        return isset( $_POST['editMenuItem_pageID'] ) ? $_POST['editMenuItem_pageID'] : NULL;
    }
    
    ## END: EDIT MENU ITEM ##
}
